//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package net.mingsoft.mdiy.action;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.mingsoft.base.entity.ResultData;
import net.mingsoft.basic.annotation.LogAnn;
import net.mingsoft.basic.bean.EUListBean;
import net.mingsoft.basic.constant.e.BusinessTypeEnum;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.basic.util.StringUtil;
import net.mingsoft.mdiy.biz.IDictBiz;
import net.mingsoft.mdiy.entity.DictEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

@Api(
        tags = {"后端-自定义模块接口"}
)
@Controller
@RequestMapping({"/${ms.manager.path}/mdiy/dict/data"})
public class DictDataAction extends BaseAction {
    @Autowired
    private IDictBiz dictBiz;

    public DictDataAction() {
    }

    @ApiIgnore
    @GetMapping({"/index"})
    public String index(HttpServletResponse response, HttpServletRequest request) {
        return "/mdiy/dict/data/index";
    }

    @ApiIgnore
    @GetMapping({"/form"})
    public String form(@ModelAttribute DictEntity dict, HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model) {
        return "/mdiy/dict/data/form";
    }

    @ApiOperation("查询字典表列表接口")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "dictType",
            value = "类型",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "dictLabel",
            value = "标签名",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "dictValue",
            value = "数据值",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "isChild",
            value = "子业务关联",
            required = false,
            paramType = "query"
    )})
    @RequestMapping(
            value = {"/list"},
            method = {RequestMethod.GET, RequestMethod.POST}
    )
    @ResponseBody
    public ResultData list(@ModelAttribute @ApiIgnore DictEntity dict, HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model) {
        if (StringUtils.isBlank(dict.getDictType())) {
            return ResultData.build().error("字典类型不能为空");
        } else if (!this.getPermissions("mdiy:dict:view", "mdiy:dictData:" + dict.getDictType() + ":settings")) {
            return ResultData.build().error("没有权限!");
        } else {
            BasicUtil.startPage();
            List dictList = this.dictBiz.query(dict);
            return ResultData.build().success(new EUListBean(dictList, (int)BasicUtil.endPage(dictList).getTotal()));
        }
    }

    @ApiOperation("获取字典详情接口")
    @ApiImplicitParam(
            name = "id",
            value = "字典编号",
            required = true,
            paramType = "query"
    )
    @GetMapping({"/get"})
    @ResponseBody
    public ResultData get(@ModelAttribute @ApiIgnore DictEntity dict, HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model) {
        if (StringUtils.isBlank(dict.getId())) {
            return ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("dict.id")}));
        } else {
            DictEntity _dict = (DictEntity)this.dictBiz.getById(dict.getId());
            if (StringUtils.isBlank(_dict.getDictType())) {
                return ResultData.build().error("字典类型不能为空");
            } else {
                return !this.getPermissions("mdiy:dict:view", "mdiy:dictData:" + _dict.getDictType() + ":settings") ? ResultData.build().error("没有权限!") : ResultData.build().success(_dict);
            }
        }
    }

    @ApiOperation("保存字典接口")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "dictLabel",
            value = "标签名",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "dictType",
            value = "类型",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "dictValue",
            value = "数据值",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "dictDescription",
            value = "描述",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "dictSort",
            value = "排序（升序）",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "isChild",
            value = "子业务关联",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "dictRemarks",
            value = "备注信息",
            required = false,
            paramType = "query"
    )})
    @LogAnn(
            title = "保存字典接口",
            businessType = BusinessTypeEnum.INSERT
    )
    @PostMapping({"/save"})
    @ResponseBody
    public ResultData save(@ModelAttribute @ApiIgnore DictEntity dict, HttpServletResponse response, HttpServletRequest request) {
        if (!this.getPermissions("mdiy:dict:save", "mdiy:dictData:" + dict.getDictType() + ":settings")) {
            return ResultData.build().error("没有权限!");
        } else if (this.dictBiz.getByTypeAndLabelAndValue(dict.getDictType(), dict.getDictLabel(), (String)null) != null) {
            return ResultData.build().error(this.getResString("diy.dict.type.and.label.repeat"));
        } else if (this.dictBiz.getByTypeAndLabelAndValue(dict.getDictType(), (String)null, dict.getDictValue()) != null) {
            return ResultData.build().error(this.getResString("diy.dict.type.and.value.repeat"));
        } else {
            this.dictBiz.saveEntity(dict);
            if (StringUtil.isBlank(dict.getDictValue())) {
                dict.setDictValue(dict.getId());
                this.dictBiz.updateEntity(dict);
            }

            return ResultData.build().success();
        }
    }

    @ApiOperation("批量删除字典")
    @LogAnn(
            title = "批量删除字典",
            businessType = BusinessTypeEnum.DELETE
    )
    @PostMapping({"/delete"})
    @ResponseBody
    public ResultData delete(@RequestBody List<DictEntity> dicts, HttpServletResponse response, HttpServletRequest request) {
        int[] ids = new int[dicts.size()];

        for(int i = 0; i < dicts.size(); ++i) {
            ids[i] = Integer.parseInt(((DictEntity)dicts.get(i)).getId());
        }

        if (!this.getPermissions("mdiy:dict:del", "mdiy:dictData:" + ((DictEntity)dicts.get(0)).getDictType() + ":settings")) {
            return ResultData.build().error("没有权限!");
        } else {
            this.dictBiz.delete(ids);
            return ResultData.build().success();
        }
    }

    @ApiOperation("更新字典信息接口")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "id",
            value = "字典编号",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "dictLabel",
            value = "标签名",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "dictType",
            value = "类型",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "dictValue",
            value = "数据值",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "dictDescription",
            value = "描述",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "dictSort",
            value = "排序（升序）",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "isChild",
            value = "子业务关联",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "dictRemarks",
            value = "备注信息",
            required = false,
            paramType = "query"
    )})
    @LogAnn(
            title = "更新字典信息接口",
            businessType = BusinessTypeEnum.UPDATE
    )
    @PostMapping({"/update"})
    @ResponseBody
    public ResultData update(@ModelAttribute @ApiIgnore DictEntity dict, HttpServletResponse response, HttpServletRequest request) {
        if (!this.getPermissions("mdiy:dict:update", "mdiy:dictData:" + dict.getDictType() + ":settings")) {
            return ResultData.build().error("没有权限!");
        } else {
            DictEntity _dict = this.dictBiz.getByTypeAndLabelAndValue(dict.getDictType(), dict.getDictLabel(), (String)null);
            if (_dict != null && !_dict.getId().equals(dict.getId())) {
                return ResultData.build().error(this.getResString("diy.dict.type.and.label.repeat"));
            } else {
                DictEntity _dict2 = this.dictBiz.getByTypeAndLabelAndValue(dict.getDictType(), (String)null, dict.getDictValue());
                if (_dict2 != null && !_dict2.getId().equals(dict.getId())) {
                    return ResultData.build().error(this.getResString("diy.dict.type.and.value.repeat"));
                } else {
                    if (StringUtils.isBlank(dict.getDictValue())) {
                        dict.setDictValue((String)null);
                    }

                    this.dictBiz.updateEntity(dict);
                    return ResultData.build().success();
                }
            }
        }
    }
}
